﻿// LoginDlg.cpp: 实现文件
//

#include "pch.h"
#include "afxdialogex.h"
#include "LoginDlg.h"
#include "resource.h"
#include "ProjectAA.h"

// CLoginDlg 对话框

IMPLEMENT_DYNAMIC(CLoginDlg, CDialogEx)

CLoginDlg::CLoginDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_LOGIN, pParent)
{

}

CLoginDlg::~CLoginDlg()
{
}

void CLoginDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_RADIO_EMPLY, radioEmploy);
	DDX_Control(pDX, IDC_RADIO_ADMIN, radioAdmin);
	DDX_Control(pDX, IDC_EDIT_PASSWORD, editPassword);
	DDX_Control(pDX, IDC_BUTTON_UP_PASSWORD, buttonUpPassword);
	DDX_Control(pDX, IDC_EDIT_USERNAME, editUserName);
}


BEGIN_MESSAGE_MAP(CLoginDlg, CDialogEx)
	ON_BN_CLICKED(IDC_RADIO_EMPLY, &CLoginDlg::OnBnClickedRadioEmply)
	ON_BN_CLICKED(IDC_RADIO_ADMIN, &CLoginDlg::OnBnClickedRadioAdmin)
	ON_BN_CLICKED(IDC_BUTTON_LOGIN, &CLoginDlg::OnBnClickedButtonLogin)
	ON_BN_CLICKED(IDC_BUTTON_LOGOUT, &CLoginDlg::OnBnClickedButtonLogout)
	ON_BN_CLICKED(IDC_BUTTON_UP_PASSWORD, &CLoginDlg::OnBnClickedButtonUpPassword)
END_MESSAGE_MAP()


// CLoginDlg 消息处理程序

void CLoginDlg::OnBnClickedRadioEmply()
{
	radioAdmin.SetCheck(FALSE);
	editPassword.EnableWindow(FALSE);
}

void CLoginDlg::OnBnClickedRadioAdmin()
{
	radioEmploy.SetCheck(FALSE);
	editPassword.EnableWindow(TRUE);
}

void CLoginDlg::OnBnClickedButtonLogin()
{
	if (radioEmploy.GetCheck())
	{
		CString userName;
		editUserName.GetWindowTextW(userName);
		if (userName.IsEmpty())
		{
			MessageBox(L"请填写用户名", L"错误", MB_OK | MB_ICONERROR);
			return;
		}
		gSystemMgr.userName = userName;
		isLogined = true;
		gSystemMgr.isAdmin = false;
		buttonUpPassword.EnableWindow(FALSE);
		MessageBox(L"员工登录成功", L"OK", MB_OK | MB_ICONEXCLAMATION);
	}
	else if (radioAdmin.GetCheck())
	{
		CString userName;
		editUserName.GetWindowTextW(userName);
		CString password;
		editPassword.GetWindowTextW(password);
		if (userName == gSystemMgr.adminName && password == gSystemMgr.adminPassword)
		{
			isLogined = true;
			gSystemMgr.isAdmin = true;
			buttonUpPassword.EnableWindow(TRUE);
			MessageBox(L"管理员登录成功", L"OK", MB_OK | MB_ICONEXCLAMATION);
		}
		else
		{
			MessageBox(L"密码错误", L"错误", MB_OK | MB_ICONERROR);
			return;
		}
	}
}

void CLoginDlg::OnBnClickedButtonLogout()
{
	isLogined = false;
	gSystemMgr.isAdmin = false;
	buttonUpPassword.EnableWindow(FALSE);
}

void CLoginDlg::OnBnClickedButtonUpPassword()
{
	if (isLogined && gSystemMgr.isAdmin)
	{
		CString newuserName;
		editUserName.GetWindowTextW(newuserName);
		CString newpassword;
		editPassword.GetWindowTextW(newpassword);
		if (newpassword.IsEmpty() || newuserName.IsEmpty())
		{
			MessageBox(L"请输入新密码", L"错误", MB_OK | MB_ICONERROR);
			return;
		}
		gSystemMgr.adminName = newuserName;
		gSystemMgr.adminPassword = newpassword;
		gSystemMgr.m_cLoginConfigIni.SaveConfigIni(L"LoginConfig", L"m_cPassword", gSystemMgr.adminName);
		gSystemMgr.m_cLoginConfigIni.SaveConfigIni(L"LoginConfig", L"m_cUserName", gSystemMgr.adminPassword);
		MessageBox(L"密码修改成功", L"OK", MB_OK | MB_ICONEXCLAMATION);
	}
	else
		MessageBox(L"请先登录管理员账号", L"错误", MB_OK | MB_ICONERROR);
}

BOOL CLoginDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	radioEmploy.SetCheck(TRUE);

	return TRUE;  // return TRUE unless you set the focus to a control
	// 异常: OCX 属性页应返回 FALSE
}
